<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avater_box">
        <img src="../assets/logo.png" alt="">
      </div>
      <!-- 登陆表单区域 -->
      <el-form 
      :model="login_form" 
      label-width="0px" 
      class="login_form"
      :rules="loginFormRules"
      ref="loginFormRef">
        <!-- v-model双向绑定指令 -->
        <!-- 用户名 -->
        <el-form-item :model="login_form" prop="username">
          <el-input v-model="login_form.username" prefix-icon="iconfont icon-user" placeholder="请输入账号"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item :model="login_form" prop="password">
          <el-input type="password" v-model="login_form.password" prefix-icon="iconfont icon-password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <!-- 按钮区 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="primary" @click="register">注册</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 这是登陆表单的数据绑定对象
      login_form:{
        username: '3018216313',
        password: '123456'
      },
      //这是表单的验证规则对象
      loginFormRules:{
        //验证用户名是否合法
        username: [
          { required: true, message: "请输入用户名", trigger: "blur"},
          { min: 10, max: 10, message: "学号长度应为10位", trigger: "blur"}
        ],
        //验证密码是否合法
        password: [
          { required: true, message: "请输密码", trigger: "blur"}
        ]
      }
    }
  },
  methods: {
    //点击按钮重置登陆表单
    resetLoginForm(){
      this.$refs.loginFormRules.resetFields();
    },
    login() {
      var login = '/login?'
      var self = this;
      login += 'username=' + this.login_form.username + '&password=' + this.login_form.password
      this.$axios
      .post(login)
      .then(function (response) {
        console.log(response.data.code);
        console.log(response.data.data.token)
        if(response.data.code === 200){
          localStorage.setItem("token", response.data.data.token);
          localStorage.setItem("id", self.login_form.username);
          window.sessionStorage.clear()
          self.$router.push('/home')
          self.$message.success('登陆成功！')
        }
      })
      .catch(function (error) {
        console.log(error);
        self.$message.error('用户名或密码不正确')
      });
    },
    register() {
      window.sessionStorage.clear()
      this.$router.push('/register')
    },
  },
}
</script>

<style lang="less" scoped>
  .login_container{
    background-color:#F6CECE;
    height: 100%;
  }

  .login_box{
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
  }

  .avater_box{
    height: 130px;
    width: 130px;
    // 盒子的边框
    border: 1px solid #eee;
    border-radius: 50%;
    // 图片和边框的内边距
    padding: 10px;
    // 边框线加阴影
    box-shadow: 0 0 10px #ddd;

    // 位置定位
    position: absolute;
    left: 50%;
    transform: translate(-50%,-50%);
    background-color: #fff;

    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }

  .btns{
    display: flex;
    justify-content: flex-end;
  }

  .login_form{
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
  }
</style>